import { ElMessage } from "element-plus";
import { ref, computed } from "vue";
import { defineStore } from "pinia";
import { loginAPI,logoutAPI } from "../apis/apis";


export const useUserStore = defineStore(
  "user",
  () => {
    // 存储登录token
    const token = ref("");

    // 登陆
    const login = async (data) => {
      const res = await loginAPI(data);
      if (res.code === 1) {
        token.value = res.data;
        return true;
      } else {
        ElNotification({
          title: 'Error',
          message: res.msg,
          type: 'error',
        })      }
      return false;
    };

    // 登出
    const logout = async () => {
      await logoutAPI();
      token.value = ''
    };

    return {
      token,
      login,
      logout
    };
  },
  {
    persist: {
      key: "user",
      storage: localStorage,
      // paths: ["token", "loginUserInfo"],
      serializer: {
        deserialize: JSON.parse,
        serialize: JSON.stringify,
      },
    },
  }
);
